package ferp.android.ads;

import android.app.Activity;
import android.view.Display;
import android.view.View;
import android.widget.Toast;
import com.amazon.device.ads.AdRegistration;
import com.appbrain.AppBrain;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import ferp.android.R;
import ferp.android.managers.BonusManager;
import ferp.android.managers.ProfileManager;
import ferp.android.views.TableView;
import ferp.core.game.Settings;
import ferp.core.log.Log;
import ferp.core.player.Profile;

/**
 * User: igorgok
 * Date: 8/26/13 1:35 PM
 */
public class MediationAdManager
{
  public static void initialize(final Activity activity)
  {
    // AppBrain SDK init
    AppBrain.init(activity.getApplicationContext());
    // Amazon ADs SDK init
    AdRegistration.setAppKey("15bd3305bd9a4adeb83d0a46031cf152");
    //AdRegistration.enableTesting(BuildConfig.DEBUG);
    //AdRegistration.enableLogging(BuildConfig.DEBUG);

    av = findAdView(activity);
    mask = findAdMask(activity);

    if (av != null)
    {
      final View mask = findAdMask(activity);

      av.setAdListener(new AdListener()
      {
        @Override
        public void onAdLoaded()
        {
          Log.debug(Log.TAG, "AD received");

          if (mask != null && av != null && av.isShown())
            mask.setVisibility(View.INVISIBLE);
        }

        @Override
        public void onAdFailedToLoad(int errorCode)
        {
          Log.debug(Log.TAG, "AD failed: " + errorCode);

          //if (mask != null)
          //  mask.setVisibility(View.GONE);
        }

        @Override
        public void onAdOpened()
        {
          Log.debug(Log.TAG, "AD opened");
        }

        @Override
        public void onAdClosed()
        {
          Log.debug(Log.TAG, "AD screen dismissed");

          Toast.makeText(activity, R.string.thanks_for_support, Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onAdLeftApplication()
        {
          Log.debug(Log.TAG, "AD left app");
        }
      });

      // set required AdMob mediation data
      //av.setAdUnitId("3b874cdb11fc4206");
      //av.setAdSize(AdSize.BANNER);

      if (BonusManager.instance(activity).isActive())
      {
        av.setVisibility(View.GONE);

        if (mask != null)
          mask.setVisibility(View.GONE);
      }
      else
        // start AD rotation
        av.loadAd(new AdRequest.Builder().build());
    }
  }

  public static void pause()
  {
    if (av != null)
      av.pause();
  }

  public static void resume()
  {
    if (av != null)
      av.resume();
  }

  public static void destroy()
  {
    if (av != null)
    {
      av.destroy();

      mask = null;
      av = null;
    }
  }

  public static void stop()
  {
    if (mask != null)
      mask.setVisibility(View.GONE);
    if (av != null)
      av.setVisibility(View.GONE);

    pause();
  }

  // AD view
  private static AdView av;
  private static View mask;

  private static AdView findAdView(Activity activity)
  {
    AdView av = (AdView)activity.findViewById(R.id.ad);
    AdView av2 = (AdView)activity.findViewById(R.id.ad2);

    if (isPortraitTooSmall(activity))
    {
      TableView table = (TableView)activity.findViewById(R.id.table);

      av.setVisibility(View.GONE);
      av2.setVisibility(View.VISIBLE);
      table.setBackgroundDrawable(null);

      return av2;
    }

    return av;
  }

  private static View findAdMask(Activity activity)
  {
    if (isPortraitTooSmall(activity))
      return null;

    return activity.findViewById(R.id.ads_mask);
  }

  private static boolean isPortraitTooSmall(Activity activity)
  {
    Profile profile = ProfileManager.instance(activity).getCurrentProfile();

    if (profile.settings.orientation == Settings.Orientation.PORTRAIT)
    {
      Display display = activity.getWindowManager().getDefaultDisplay();

      return Math.min(display.getWidth(), display.getHeight()) <= 320;
    }

    return false;
  }
}
